# config.py
"""
MicroPython 协作开发指南 - 配置文件
MicroPython Collaborative Development Guide - Configuration File

用于控制硬件模式和模拟模式的切换
Used to control switching between hardware mode and mock mode
"""

import os
from typing import Dict, Any

# 主要配置 | Main configuration
USE_MOCK_MODE = os.getenv('USE_MOCK_MODE', 'True').lower() == 'true'

# 详细配置 | Detailed configuration
CONFIG: Dict[str, Any] = {
    'mock_mode': USE_MOCK_MODE,
    'debug': os.getenv('DEBUG', 'False').lower() == 'true',
    'log_level': os.getenv('LOG_LEVEL', 'INFO'),
    
    # 硬件配置 | Hardware configuration
    'hardware': {
        'uart_baudrate': int(os.getenv('UART_BAUDRATE', '115200')),
        'i2c_frequency': int(os.getenv('I2C_FREQ', '100000')),
        'spi_baudrate': int(os.getenv('SPI_BAUDRATE', '1000000')),
    },
    
    # 模拟配置 | Mock configuration
    'mock': {
        'auto_response': os.getenv('MOCK_AUTO_RESPONSE', 'True').lower() == 'true',
        'simulate_errors': os.getenv('MOCK_SIMULATE_ERRORS', 'False').lower() == 'true',
        'response_delay': float(os.getenv('MOCK_RESPONSE_DELAY', '0.01')),
    }
}

def get_config(key: str, default: Any = None) -> Any:
    """
    获取配置值 | Get configuration value
    
    参数 Parameters:
    - key (str): 配置键名 | Configuration key
    - default (Any): 默认值 | Default value
    
    返回值 Returns:
    - Any: 配置值 | Configuration value
    """
    keys = key.split('.')
    value = CONFIG
    
    for k in keys:
        if isinstance(value, dict) and k in value:
            value = value[k]
        else:
            return default
            
    return value
